<template>
  <div class="regist_container">
    <el-card class="box-card">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名:" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <!--   <el-form-item label="年龄" prop="age">
          <el-input v-model.number="ruleForm.age"></el-input>
        </el-form-item> -->
        <el-form-item label="身份:">
          <el-radio-group v-model="ruleForm.role" prop="role">
            <el-radio label="1">学生</el-radio>
            <el-radio label="2">招聘方</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
        <el-link type="primary" @click="$router.push('/login')">注册成功?去登录页面?</el-link>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { studentRegistAPI } from '@/api/studentAPI/studentAPI.js'
import { recruiterRegistAPI } from '@/api/recruiterAPI/recruiterAPI.js'
//引入注册表单验证js
// import registValidate from '@/utils/registValidate.js'
export default {
  data() {
    //用户名验证
    var validateUsername = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }

    //年龄验证
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('年龄不能为空'))
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'))
        } else {
          if (value < 18) {
            callback(new Error('必须年满18岁'))
          } else {
            callback()
          }
        }
      }, 1000)
    }

    //密码验证
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }

    //确认密码验证
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        username: '',
        password: '',
        checkPass: '',
        age: '',
        role: '1'
      },
      //表单验证规则
      rules: {
        password: [{ validator: validatePass, trigger: 'blur' }],
        checkPass: [{ validator: validatePass2, trigger: 'blur' }],
        age: [{ validator: checkAge, trigger: 'blur' }],
        username: [{ validator: validateUsername, trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          console.log(this)
          //1 表示为学生用户
          if (this.ruleForm.role === '1') {
            const res = await studentRegistAPI(this.ruleForm.username, this.ruleForm.password)
            // console.log(res.data.data)
            //res.data.data为null时注册失败
            if (!res.data.data) {
              //提示错误信息
              this.$message.error('该用户已存在!')
              //重置表单
              this.resetForm(formName)
            } else {
              // 注册成功,提醒是否跳转到登录界面
              this.$message.success('注册成功!')
            }
          } else {
            //2 表示为招聘方用户
            const res = await recruiterRegistAPI(this.ruleForm.username, this.ruleForm.password)
            // console.log(res.data.data)
            //res.data.data为null时注册失败
            if (!res.data.data) {
              //提示错误信息
              this.$message.error('该用户已存在!')
              //重置表单
              this.resetForm(formName)
            } else {
              // 注册成功,提醒是否跳转到登录界面
              this.$message.success('注册成功!')
            }
          }
        } else {
          // 验证失败
          console.log('error submit!!')
          return false
        }
      })
    },
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style>
.box-card {
  width: 480px;
  margin: 50px auto;
}
</style>
